//服务层
const mongoose = require("mongoose");
module.exports.getGoods3 = async ({ currentPage, pageSize,categoryId }) => {
    if(categoryId != ''){
        const search = await  mongoose.model("goodsModel").find({$and:[{categoryId},{istake:{$ne:'2'}}]});
    const total = search.length;
    const pages = Math.ceil(total / pageSize);//计算食品列表的总页数
    const goodsData = await mongoose.model("goodsModel")
        .find({$and:[{categoryId},{istake:{$ne:'2'}}]})
        .populate("categoryId")
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);
    return { total, pages, goodsData};
    }else{
    const search = await  mongoose.model("goodsModel").find({istake:{$ne:'2'}});
    const total = search.length;
    const pages = Math.ceil(total / pageSize);//计算食品列表的总页数
    const goodsData = await mongoose.model("goodsModel")
        .find({istake:{$ne:'2'}})
        .populate("categoryId")
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);
    return { total, pages, goodsData};
    }
    
}
module.exports.getAllGoods3 = async (res,req) => {
    const goodsData = await mongoose.model("goodsModel").find()
    return {goodsData };
}

module.exports.getOne3 = async (_id) => {
    return await mongoose.model("goodsModel").find(_id);
}

module.exports.updateOne3 = async ({ _id, name, price, brief, categoryId, stock, previewSrc, detailsSrc, istake }) => {
    return await mongoose.model("goodsModel").updateOne({ _id }, { name, price, brief, categoryId, stock, previewSrc, detailsSrc, istake });
}
module.exports.deleteOne3 = async ({ _id,istake }) => {
    return await mongoose.model("goodsModel").updateOne({ _id }, { istake });
}

module.exports.addGoods3 = async (goods) => {
    return await mongoose.model("goodsModel").create(goods);
}

